package com.xukai.springboot.service.statistics;

import com.xukai.springboot.datalayer.statis.DataSourceMapper;
import com.xukai.springboot.entity.statistics.t_statis_datasource;
import com.xukai.springboot.util.cache.LocalCache;
import com.xukai.springboot.util.config.DatabaseContextHolder;
import com.xukai.springboot.util.constants.DataBaseType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DataSourceService {

    @Autowired
    DataSourceMapper dataSourceMapper;

    @Autowired
    DataCaptureService dataCaptureService;


    /**
     * 获取数据源列表
     */
    public List<t_statis_datasource> GetDataSourceList(int state, String startTime, String endTime,
                                                       String dataSourceName, int pageSize, int startCount) {
        DatabaseContextHolder.setDB(DataBaseType.manager.name());
        return dataSourceMapper.GetDataSourceList(state, startTime, endTime, dataSourceName, pageSize, startCount);
    }

    /**
     * @param state
     * @param startTime
     * @param endTime
     * @param dataSourceName
     * @return
     */
    public int GetDataSourceCount(int state, String startTime, String endTime,
                                  String dataSourceName) {
        DatabaseContextHolder.setDB(DataBaseType.manager.name());
        return dataSourceMapper.GetDataSourceCount(state, startTime, endTime, dataSourceName);
    }

    /**
     * 新建数据源
     */
    public t_statis_datasource AddDataSource(t_statis_datasource data) {
        Date currentDate = new Date();
        data.setLastUpdateTime(currentDate);
        data.setCreateTime(currentDate);
        DatabaseContextHolder.setDB(DataBaseType.manager.name());
        dataSourceMapper.AddDataSource(data);
        //清空缓存
        ClearCache();
        return data;
    }

    /**
     * 修改数据源
     */
    public void UpdateDataSource(t_statis_datasource data) {
        DatabaseContextHolder.setDB(DataBaseType.manager.name());
        dataSourceMapper.UpdateDataSource(data);
        //清空缓存
        ClearCache();
    }

    /**
     * 删除数据源
     */
    public void DelDataSource(int id) {
        DatabaseContextHolder.setDB(DataBaseType.manager.name());
        dataSourceMapper.DelDataSource(id);
        //清空缓存
        ClearCache();
    }

    /**
     * 获取数据源详细信息
     */
    public t_statis_datasource GetDataSource(int id) {
        DatabaseContextHolder.setDB(DataBaseType.manager.name());
        return dataSourceMapper.GetDataSource(id);
    }

    /**
     * 修改数据源状态
     */
    public void UpdateState(int id, int state) {
        DatabaseContextHolder.setDB(DataBaseType.manager.name());
        dataSourceMapper.UpdateState(id, state);
        //清空缓存
        ClearCache();
    }


    public void ClearCache() {
        LocalCache.GetInstance().RemoveCache("Statis_DataSource");
    }

    /**
     * 判断数据源是否存在
     *
     * @param id
     * @param dataSourceName
     * @return
     */
    public Boolean IsExist(int id, String dataSourceName) {
        DatabaseContextHolder.setDB(DataBaseType.manager.name());
        int count = dataSourceMapper.IsExist(id, dataSourceName);
        if (count == 0) {
            return false;
        }
        return true;
    }

    /**
     * 刷新数据
     *
     * @param dataSourceName
     * @param resultType
     * @param id
     * @param sql
     */
    public void RefreshData(String dataSourceName, int resultType, int id, String sql) {
        dataCaptureService.RefreshData(dataSourceName, resultType, id, sql);
        ClearCache();
    }


}
